# -*- coding: utf-8 -*-

# 导入必要的工具包
# 独立调用xgboost或在sklearn框架下调用均可。
# 1. 模型训练：超参数调优
#     1. 初步确定弱学习器数目： 20分
#     2. 对树的最大深度（可选）和min_children_weight进行调优（可选）：20分
#     3. 对正则参数进行调优：20分
#     4. 重新调整弱学习器数目：10分
#     5. 行列重采样参数调整：10分
# 2. 调用模型进行测试10分
# 3. 生成测试结果文件10分

import xgboost as xgb
from xgboost import XGBClassifier

from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import StratifiedKFold

from sklearn.metrics import log_loss

import pandas as pd
import numpy as np

dpath = './data/'
train = pd.read_csv(dpath + 'RentListingInquries_FE_train_sample.csv')

train_X = train.drop("interest_level", axis=1)
train_y = train["interest_level"]

# # 各类样本不均衡，交叉验证是采用StratifiedKFold，在每折采样时各类样本按比例采样
# # prepare cross validation
from sklearn.model_selection import StratifiedKFold
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=3)

#第一轮参数调整得到的n_estimators 是 277
estimators = 50

xgb3 = XGBClassifier(
        learning_rate =0.1,
        n_estimators=estimators,
        max_depth=4,
        min_child_weight=1,
        gamma=0,
        subsample=0.3,
        colsample_bytree=0.8,
        colsample_bylevel = 0.7,
        objective= 'multi:softmax',
        nthread=-1,
        seed=3)

# 上一轮求得的参数是： {'max_depth': 3, 'min_child_weight': 3}
max_depth2 = [4,5,6]
min_child_weight2 = [1,2]
param_test_3 = dict(max_depth=max_depth2, min_child_weight=min_child_weight2)

gsearch3 = GridSearchCV(xgb3, param_grid = param_test_3, scoring='neg_log_loss',n_jobs=-1, cv=kfold)
gsearch3.fit(train_X , train_y)

print gsearch3.grid_scores_
print gsearch3.best_params_
print gsearch3.best_score_

# FIXME
# params: {'max_depth': 2, 'min_child_weight': 4}

# 2/4
# [mean: -0.64460, std: 0.01498, params: {'max_depth': 2, 'min_child_weight': 2}, mean: -0.64347, std: 0.01400, params: {'max_depth': 2, 'min_child_weight': 3}, mean: -0.64252, std: 0.01468, params: {'max_depth': 2, 'min_child_weight': 4}, mean: -0.64723, std: 0.01750, params: {'max_depth': 3, 'min_child_weight': 2}, mean: -0.64653, std: 0.01559, params: {'max_depth': 3, 'min_child_weight': 3}, mean: -0.64926, std: 0.01803, params: {'max_depth': 3, 'min_child_weight': 4}, mean: -0.66681, std: 0.01751, params: {'max_depth': 4, 'min_child_weight': 2}, mean: -0.66589, std: 0.01781, params: {'max_depth': 4, 'min_child_weight': 3}, mean: -0.66170, std: 0.01908, params: {'max_depth': 4, 'min_child_weight': 4}]
# {'max_depth': 2, 'min_child_weight': 4}
# -0.642520629989


# [mean: -0.65632, std: 0.00775, params: {'max_depth': 4, 'min_child_weight': 1}, mean: -0.65776, std: 0.00926, params: {'max_depth': 4, 'min_child_weight': 2}, mean: -0.65876, std: 0.01208, params: {'max_depth': 5, 'min_child_weight': 1}, mean: -0.66143, std: 0.01495, params: {'max_depth': 5, 'min_child_weight': 2}, mean: -0.66430, std: 0.01092, params: {'max_depth': 6, 'min_child_weight': 1}, mean: -0.66265, std: 0.00682, params: {'max_depth': 6, 'min_child_weight': 2}]
# {'max_depth': 4, 'min_child_weight': 1}
# -0.656323128822

from sklearn.metrics import accuracy_score
xgb_param = xgb3.get_xgb_params()
xgb_param['num_class'] = 3
# 直接调用xgboost，而非sklarn的wrapper类
# num_round = 2
dtrain = xgb.DMatrix(train_X, label=train_y)
bst = xgb.train(xgb_param, dtrain, 5)
train_predictions = bst.predict(dtrain)
y_train = dtrain.get_label()
train_accuracy = accuracy_score(y_train, train_predictions)
print ("Train Accuary: %.2f%%" % (train_accuracy * 100.0))

# Train Accuary: 72.00%

